package client.pqueue;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class QClientHandler extends IoHandlerAdapter {

	private final static Logger LOGGER = LoggerFactory
			.getLogger(QClientHandler.class);

	private String host;
	private int port;
	private long timeout;

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public long getTimeout() {
		return timeout;
	}

	public void setTimeout(long timeout) {
		this.timeout = timeout;
	}

	public QClientHandler(String sHost, int sPort, long sTimeout) {
		this.host = sHost;
		this.port = sPort;
		this.timeout = sTimeout;
	}

	public QClientHandler() {
	}

	@Override
	public void sessionOpened(IoSession session) {
		LOGGER.info("connected!");
	}

	@Override
	public void messageReceived(IoSession session, Object message) {
	}

	@Override
	public void exceptionCaught(IoSession session, Throwable cause) {
		session.close(true);
	}

	@Override
	public void sessionClosed(IoSession session) throws Exception {
		Thread.sleep(5000);
		QClient.connect(host, port, timeout);
	}

}